package com.cutthroatcreations.woodenrailway;

import java.util.ArrayList;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import com.cutthroatcreations.woodenrailway.CustomHttpClient;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class WoodenrailwayActivity extends Activity {

	private static final String TAG = "WoodenrailwayActivityLog";
	private String trains;
	private User user;
	ProgressDialog progressDialog;
	EditText un, pw;
	Button ok;
	Button signUp;
	Context context;

	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		WoodenApplication.clearCache();
		context = WoodenApplication.getContext();
		
		un = (EditText) findViewById(R.id.editEmail);
		pw = (EditText) findViewById(R.id.editPassword);
		ok = (Button) findViewById(R.id.login);
		signUp = (Button) findViewById(R.id.signUpBtn);
		if(checkConnection(context)){
		ok.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				user = new User(un.getText().toString(), pw.getText().toString());
				AsyncTask<String, Void, String> triansTask = new trainJson(
						WoodenrailwayActivity.this);

				triansTask.execute("users/login", user.getEmail(), user.getPw());

			}
		});
		signUp.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {

				Intent intent = new Intent(getApplication(),
						SignUpActivity.class);

				startActivity(intent);

			}
		});
		}else{
			
			ok.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					Toast.makeText(context, "Please Turn On Data or Wifi",
							Toast.LENGTH_LONG).show();
			
				}
			});
			signUp.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {

					Toast.makeText(context, "Please Turn On Data or Wifi",
							Toast.LENGTH_LONG).show();

				}
			});
			
		}

	}

	private boolean checkConnection(Context context) {
		final ConnectivityManager cm = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		final NetworkInfo networkInfo = cm.getActiveNetworkInfo();
		if (networkInfo == null || !networkInfo.isConnectedOrConnecting()) {
			Toast.makeText(context, R.string.no_network_connection_toast,
					Toast.LENGTH_LONG).show();
			Log.e(TAG, "checkConnection - no connection found");
			return false;
		}
		return true;
	}

	public class trainJson extends AsyncTask<String, Void, String> {
		private String type;
		public WoodenrailwayActivity activity;

		public trainJson(WoodenrailwayActivity a) {

			activity = a;

		}

		@Override
		protected void onPreExecute() {
			progressDialog = ProgressDialog.show(activity, "LOGGING IN",
					"Please Wait....", true);
		};

		@Override
		protected String doInBackground(String... params) {

			ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
			postParameters.add(new BasicNameValuePair("email", params[1]
					.toString()));
			postParameters.add(new BasicNameValuePair("password", params[2]
					.toString()));
			postParameters.add(new BasicNameValuePair("app", "1"));

			try {
				String res = null;
				String response = null;

				response = CustomHttpClient.executeHttpPost(params[0],
						postParameters);

				res = response.toString();
				res = res.replaceAll("\\s+", "");
				res = res.trim();
				Log.i(TAG, res);
				return res;

			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}

		}

		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			if (result == null) {
				result = "Hi";
			}
			trains = result;

			Intent intent = new Intent(getApplication(), MainActivity.class);

			intent.putExtra("trains", trains);
			intent.putExtra("email", user.getEmail());
			intent.putExtra("password", user.getPw());

			startActivity(intent);
			progressDialog.dismiss();

		}

	}

}